//
//  ViewController.m
//  AVFoundation
//
//  Created by bao on 2018/6/20.
//  Copyright © 2018年 bao. All rights reserved.
//

#import "ViewController.h"

#import <AVFoundation/AVFoundation.h>

#import "SpeechSynthesizerViewController.h"
#import "AudioPlayerViewController.h"
#import "AudioRecorderViewController.h"
#import "PlayerViewController.h"
#import "AVKitViewController.h"

@interface ViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSArray *data;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.title = @"AVFoundation";
    
    self.data = @[@{@"title" : @"媒体播放",
                    @"items" : @[@"AVSpeechSynthesizer",
                                 @"AVAudioPlayer",
                                 @"AVAudioRecorder",
                                 @"AVPlayer",
                                 @"AVKit"]
                    },
                  @{@"title" : @"媒体捕捉",
                    @"items" : @[@"基本捕捉"]
                    },
                  @{@"title" : @"Example",
                    @"items" : @[@"录制和播放语音",
                                 @"自定义播放器"]
                    }];
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
    tableView.tableFooterView = [[UIView alloc] init];
    tableView.dataSource = self;
    tableView.delegate = self;
    [self.view addSubview:tableView];
}

#pragma mark - UITableViewDataSource, UITableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.data.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *items = self.data[section][@"items"];
    return items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"cellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    NSArray *items = self.data[indexPath.section][@"items"];
    NSString *item = items[indexPath.row];
    cell.textLabel.text = item;
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *title = self.data[section][@"title"];
    return title;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSString *item = self.data[indexPath.section][@"items"][indexPath.row];
    if ([item isEqualToString:@"AVSpeechSynthesizer"]) {
        SpeechSynthesizerViewController *vc = [[SpeechSynthesizerViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    } else if ([item isEqualToString:@"AVAudioPlayer"]) {
        AudioPlayerViewController *vc = [[AudioPlayerViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    } else if ([item isEqualToString:@"AVAudioRecorder"]) {
        AudioRecorderViewController *vc = [[AudioRecorderViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    } else if ([item isEqualToString:@"AVPlayer"]) {
        PlayerViewController *vc = [[PlayerViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    } else if ([item isEqualToString:@"AVKit"]) {
        AVKitViewController *vc = [[AVKitViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

@end
